package com.company.cloud.user.modules.app.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseParam;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.user.constant.ErrorUserEnum;
import com.company.cloud.user.constant.InviteStateTypeEnum;
import com.company.cloud.user.constant.InviteTypeEnum;
import com.company.cloud.user.modules.api.response.UserSimple;
import com.company.cloud.user.modules.api.service.UserRemoteService;
import com.company.cloud.user.modules.base.entity.TeamInfo;
import com.company.cloud.user.modules.base.entity.TeamUserInfo;
import com.company.cloud.user.modules.base.entity.UserInvitationInfo;
import com.company.cloud.user.modules.base.service.TeamInfoService;
import com.company.cloud.user.modules.base.service.TeamUserInfoService;
import com.company.cloud.user.modules.base.service.UserInvitationInfoService;
import com.company.cloud.user.modules.cache.models.TeamInvitationModel;
import com.company.cloud.user.modules.cache.models.TeamModel;
import com.company.cloud.user.modules.cache.models.TeamUserModel;
import com.company.cloud.user.modules.cache.models.UserInvitationModel;
import com.company.cloud.user.modules.cache.service.TeamCacheService;
import com.company.cloud.user.modules.cache.utils.UserConvertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.*;

@Log4j2
@Login
@RestController
@RequestMapping("/app/user/team")
@Api(tags="User_App_团队服务")
public class TeamController {
	
	@Autowired
	private TeamInfoService teamInfoService;
	@Autowired
	private TeamUserInfoService teamUserInfoService;
	@Qualifier("userRemoteServiceImpl")
	@Autowired
	private UserRemoteService userRemoteService;
	@Autowired
	private UserInvitationInfoService userInvitationInfoService;
	@Autowired
	private TeamCacheService teamCacheService;

	@GetMapping("/teamList")
	@ApiOperation(value="我的团队", response=TeamModel.class)
	public BaseResult teamList(BaseParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		List<TeamInfo> teamInfoList = teamInfoService.list(new QueryWrapper<TeamInfo>()
				.inSql("id", StrUtil.format("select team_id from cust_team_user_info where user_id={}", userInfo.getId()))
		);
		if(CollectionUtils.isEmpty(teamInfoList)) {
			return BaseResult.ok();
		}
		return BaseResult.ok(teamCacheService.getTeamModelCache(teamInfoList));
	}

	@GetMapping("/teamInviteList")
	@ApiOperation(value="我的团队邀请", response=TeamModel.class)
	public BaseResult teamInviteList(BaseParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		List<UserInvitationInfo> invitationList = userInvitationInfoService.list(new QueryWrapper<UserInvitationInfo>()
					.eq("invitation_user_id", userInfo.getId())
					.eq("invitation_type", InviteTypeEnum.TEAM.getCode())
					.eq("rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
		);
		if(CollectionUtils.isEmpty(invitationList)) {
			return BaseResult.ok();
		}
		List<UserInvitationModel> list = new ArrayList<>();
		for (UserInvitationInfo info: invitationList) {
			list.add(UserConvertUtils.getUserInvitationModel(info));
		}
		return BaseResult.ok(list);
	}

	@GetMapping("/teamUserList")
	@ApiOperation(value="团队成员列表", response=TeamModel.class)
	public BaseResult teamUserList(@RequestParam("teamId") Integer teamId) {
		TeamInfo teamInfo = teamInfoService.getById(teamId);
		TeamModel teamModel = teamCacheService.getTeamModelCache(teamInfo);
		teamModel = teamCacheService.getTeamModelWithTeamUser(teamModel);
		return BaseResult.ok(teamModel);
	}

	@GetMapping("/inviteCheck")
	@ApiOperation(value="团队邀请成员验证", response=BaseResult.class)
	public BaseResult inviteCheck(@RequestParam("inviteId") String inviteId) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		UserInvitationInfo invitation = userInvitationInfoService.getById(inviteId);
		if (invitation == null
				|| BaseConstant.EnabledEnum.DISABLED.getCode().equals(invitation.getRowsState())
				|| invitation.getOverDate().isBefore(LocalDateTime.now())
				|| !InviteTypeEnum.TEAM.getCode().equals(invitation.getInvitationType())
				|| !InviteStateTypeEnum.UNCONFIRMED.getCode().equals(invitation.getInvitationState())
		) {
			throw new BusinessException(ErrorUserEnum.Invitation_Error);
		}

		TeamUserInfo teamUserInfo = new TeamUserInfo();
		teamUserInfo.setCompanyNo(invitation.getCompanyNo());
		teamUserInfo.setTeamId(invitation.getTeamId());
		teamUserInfo.setUserId(userInfo.getId());
		teamUserInfo.addCreateParam(userInfo);
		teamUserInfo.addEditParam(userInfo);
		teamUserInfoService.save(teamUserInfo);
		return BaseResult.ok();
	}

}
